/*
*    Phantom Test Driver v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
/*
 * SuiteRunBase.java
 *
 * Created on December 31, 2005, 5:31 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package ptd_suite_objects;

import java.util.Vector;
import ptd.Main;
import ptd.PTDDisplay;
import ptd.PTDHTMLPane;
import ptd.PTDRemoteMachine;
import ptd.PTDScriptComm;

/**
 *
 * @author John
 */
public class SuiteRunBase extends Object{
    
    /** Creates a new instance of SuiteRunBase */
    public SuiteRunBase(SuiteBase inObject) {
        ChildNodes = new Vector<SuiteRunBase>();
        myObject = inObject;
    }
    
    public void run(PTDDisplay inDisplay, PTDScriptComm inComm, boolean bIsRemote, PTDRemoteMachine Machine){
        if(bIsRemote){
            if(Machine.runState == false){
                return;
            }            
        }else{
            if(Main.getMainFrame().getSuiteRunning() == false){
                return;
            }
        }
        
        int i = ChildNodes.size();
        myObject.initRun(inDisplay);
        
        if(inDisplay instanceof PTDHTMLPane){
            if(bIsRemote){
                PTDHTMLPane aDisp = (PTDHTMLPane)inDisplay;
                aDisp.setRemoteMachine(Machine);
            }
        }
                
        myObject.runPreprocessor(inDisplay, inComm);        
        
        if(bIsRemote){
            if(Machine.runState == false){
                inDisplay.stopSuite();
                return;
            }
        }else{
            if(Main.getMainFrame().getSuiteRunning() == false){
                inDisplay.stopSuite();
                return;
            }
        }
        
        if(bIsRemote){
            myObject.runRemote(inDisplay, inComm, Machine);
        }else{
            myObject.run(inDisplay, inComm);
        }
        
        if(i > 0){
            // Run this regardless, since Suites may display some things
            int ctr = 0;
            for(ctr = 0; ctr < i; ctr++){
                SuiteRunBase aBase = (SuiteRunBase)ChildNodes.get(ctr);
                Main.debug_out("Running: " + aBase.getObject().myName + " (Parent: " + myObject.myName + ")");
                aBase.run(inDisplay, inComm, bIsRemote, Machine);
                
                if(bIsRemote){
                    if(Machine.runState == false){
                        inDisplay.stopSuite();
                        return;
                    }
                }else{
                    if(Main.getMainFrame().getSuiteRunning() == false){
                        inDisplay.stopSuite();
                        return;
                    }
                }                
                //if(aBase.run(inDisplay, inComm) <= 0){
                    //return 0;
                //}
            }
        }else{
            // Check if myObject is script and uses Phantom
            // Each Object will have a run, and be passed something to output to...
        }
        
        if(bIsRemote){
            if(Machine.runState == false){
                inDisplay.stopSuite();
                return;
            }
        }else{
            if(Main.getMainFrame().getSuiteRunning() == false){
                inDisplay.stopSuite();
                return;
            }
        }
        
        myObject.runPostprocessor(inDisplay, inComm);
        inDisplay.stopSuite();
        //return 1;
        // Check for Children
            // Run Children
            // Sum Error/Warn Count
        //Else
            // Run Self
            // Collect Error/Warn Count
        //
    }
    
    public void addNode(SuiteRunBase inNode){
        ChildNodes.add(inNode);
    }
    
    public SuiteBase getObject(){
        return myObject;
    }
    
    // We cant just use the treeview objects because cant inject in a thread, so these contain copies of the objects
    private SuiteBase myObject;
    private Vector<SuiteRunBase> ChildNodes;
    
}
